package jk.fox.invocation;

import jk.fox.node.client.RpcCluster;
import jk.fox.common.payload.container.RpcContainer;
import jk.fox.engine.RpcNodeDefinition;
import jk.fox.invocation.invoker.Invoker;
import jk.fox.invocation.proxy.ProxyFactory;

import java.lang.reflect.Method;

/**
 * @author cuichao
 * @Description: RpcInvocation
 * @Date: create in 2020/5/16 21:57
 */
public class RpcInvocation implements Invocation{

    private RpcContainer container;

    private ProxyFactory proxyFactory;

    private Invoker invoker;

    private RpcCluster cluster;


    public RpcInvocation(Invoker invoker, ProxyFactory proxyFactory, RpcContainer container){
        this.invoker = invoker;
        this.container = container;
        this.proxyFactory = proxyFactory;
    }
    @Override
    public Object remote(String key, String method, String[] parameterTypes, Object[] parameters) throws Exception {
        Object provider = container.getService(key);
        Object invoke = invoker.invoke(provider, method, parameterTypes, parameters);
        return invoke;
    }
    @Override
    public Object local(String key, Method method, Object[] parameters) throws Exception {
        Object provider = container.getService(key);
        Object invoke = invoker.invoke(provider, method,parameters);
        return invoke;
    }

    @Override
    public Object proxy(RpcNodeDefinition definition) {
        return proxyFactory.getProxy(definition,this);

    }

    @Override
    public RpcCluster cluster() {
        return cluster;
    }

    @Override
    public void cluster(RpcCluster rpcCluster) {
        this.cluster = rpcCluster;
    }


    @Override
    public RpcContainer container() {
        return container;
    }


}
